import { NextResponse } from "next/server";
// const { URL } = require("url");

export default function myMiddleware(request) {
  // 中间逻辑
  if (request.headers) {
    const acceptLanguageEntry = request.headers.get("accept-language");
    console.log(acceptLanguageEntry);
    // 中文
    if (
      acceptLanguageEntry &&
      acceptLanguageEntry.split(",")[0].includes("zh")
    ) {
      // 如果包含 "zh"，则设置重定向路径为中文页面
      const url = request.nextUrl.clone();
      url.pathname = `/zh${url.pathname}`;
      return NextResponse.rewrite(new URL(url));
    }
  }
  return NextResponse.next(); // 执行下一个中间件
}

// The above middleware would only run for the "/" path
export const config = {
  matcher: "/line",
};
